﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class FLTICCGLLGReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "GBMOV";
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\HLT\HLTPlanningReport.xls");
        public FLTICCGLLGReport()
            : base()
        {
        }
        public FLTICCGLLGReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"FLT\FLTInventory.xls";
            /*
             A.ICCGLDAT,A.ICCGLPARTIE,A.ICCGLLBEHI,A.ICCGLLO,A.ICCGLLB,A.ICCGLLP,
B.GPGART,B.GPGVAR,C.GSGARNBEZ,C.GSAGARNNR,B.GPGFARBE,D.GSCBEZ,KG,NET,GROSS*/
            #region Excel  columns
            columns.Add("ICCGLDAT");
            columns.Add("ICCGLPARTIE");
            columns.Add("ICCGLLBEHI");
            columns.Add("ICCGLLO");
            columns.Add("ICCGLLB");
            columns.Add("ICCGLLP");
            columns.Add("GPGART");
            columns.Add("GPGVAR");
            columns.Add("GSGARNBEZ");
            columns.Add("GSAGARNNR");
            columns.Add("GPGFARBE");
            columns.Add("GSCBEZ");
            columns.Add("KG");
            columns.Add("NET");
            columns.Add("GROSS");
            
            #endregion 
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 7, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["ICCGLLG"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("ICCGLDAT", typeof(System.String));
                dtOutput.Columns.Add("ICCGLPARTIE", typeof(System.String));
                dtOutput.Columns.Add("ICCGLLBEHI", typeof(System.String));
                dtOutput.Columns.Add("ICCGLLO", typeof(System.String));
                dtOutput.Columns.Add("ICCGLLB", typeof(System.String));
                dtOutput.Columns.Add("ICCGLLP", typeof(System.String));
                dtOutput.Columns.Add("GPGART", typeof(System.String));
                dtOutput.Columns.Add("GPGVAR", typeof(System.String));
                dtOutput.Columns.Add("GSGARNBEZ", typeof(System.String));
                dtOutput.Columns.Add("GSAGARNNR", typeof(System.String));
                dtOutput.Columns.Add("GPGFARBE", typeof(System.String));
                dtOutput.Columns.Add("GSCBEZ", typeof(System.String));
                dtOutput.Columns.Add("KG", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("NET", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GROSS", System.Type.GetType("System.Decimal"));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 4, 1);
                int count = dtOutput.Rows.Count;
                exAgent.BatchConvert2Number(4, 13, count + 3, 15);
                exAgent.SetCellValue(1, 1, " ");
                /*foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                } */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void WriteExcelReport(string fileType, string STARTDATE)
        {
            try
            {
                string filename = "";
                string sheetName = "";
                string Standard = "ICCGLLG";
                strExcelFile = @"FLT\FLTStandard.xls";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);

                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"Financial\历史库存_" + filedate + ".xls";
                string file = Path.Combine(PathHelper.ApplicationPath, @"Report\" + filename);
                sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                exAgent.CreateNewSheet(filename, Standard, sheetName);

                exAgent.SetCellValue(1, 3, STARTDATE);
                FillInExcelData(columns, dsSource, 4, 1);
                exAgent.Save();
                exAgent.Close();
                System.Diagnostics.Process.Start("EXCEL.EXE", file);
                //base.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
